package utils;

import java.math.BigInteger;
import java.sql.Date;


/*******************************************************************************
 * Filename : StringUtils.java Decription : Các tiện ích về String cho project
 * Created date : Jun 2, 2008 Author : Phonglt
 ******************************************************************************/
public class StringUtils {

	/***************************************************************************
	 * Method name : encryptString Return type : String Decription : Mã hóa
	 * password Created date : May, 2008 Author : Honglk
	 **************************************************************************/
	public static String encryptString(String strOrigin) {
		String result = "";
		int iRow, iCol;
		int iAsc;
		String sHex;
		int iLength = strOrigin.length();
		String[][] sEncrytation = new String[8][8];
		try {
			// Init
			for (iRow = 0; iRow <= 7; iRow++) {
				for (iCol = 0; iCol <= 7; iCol++) {
					sEncrytation[iRow][iCol] = sSampleED[iRow + iCol];
				}
			}
			// Encrypt Input
			sEncrytation[7][0] = String.valueOf(iLength / 10);
			sEncrytation[0][7] = String.valueOf(iLength % 10);
			if (iLength > 0) {
				for (iRow = 2; iRow <= 5; iRow++) {
					for (iCol = 0; iCol <= 7; iCol = iCol + 2) {
						iAsc = (int) strOrigin.charAt(0);
						strOrigin = strOrigin.substring(1, iLength);
						iLength = strOrigin.length();
						sHex = Integer.toHexString(iAsc);
						sEncrytation[iRow][iCol] = sHex.substring(0, 1);
						sEncrytation[iRow][iCol + 1] = sHex.substring(1, 2);
						if (iLength == 0) {
							iCol = 10;
							iRow = 10;
						}
					}
				}
			}
			// Return
			for (iRow = 0; iRow <= 7; iRow++) {
				for (iCol = 0; iCol <= iRow; iCol++) {
					result = result + sEncrytation[iRow - iCol][iCol];
				}
			}
			for (iRow = 0; iRow <= 6; iRow++) {
				for (iCol = 0; iCol <= (6 - iRow); iCol++) {
					result = result + sEncrytation[7 - iCol][iRow + 1 + iCol];
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
	/**************************************
	 * Method name		: decryptString
	 * Return type		: String
	 * Decription		:Giai ma du lieu
	 * Created date		: Jun 7, 2008
	 * Author			: Honglk
	 **************************************/
	public static String decryptString(String strOrigin) {
		String result = "";
		int iRow, iCol;
		int iAsc;
		String sHex;
		int iLength = strOrigin.length();
		String[][] sEncrytation = new String[8][8];
		try {
			// Init
			if (iLength == 0)
				return result;
			for (iRow = 0; iRow <= 7; iRow++) {
				for (iCol = 0; iCol <= 7; iCol++) {
					sEncrytation[iRow][iCol] = sSampleED[iRow + iCol];
				}
			}
			// Encrypt Input
			for (iRow = 0; iRow <= 7; iRow++) {
				for (iCol = 0; iCol <= iRow; iCol++) {
					sEncrytation[iRow - iCol][iCol] = strOrigin.substring(0, 1);
					strOrigin = strOrigin.substring(1, iLength);
					iLength = strOrigin.length();
				}
			}
			for (iRow = 0; iRow <= 6; iRow++) {
				for (iCol = 0; iCol <= (6 - iRow); iCol++) {
					sEncrytation[7 - iCol][iRow + 1 + iCol] = strOrigin
							.substring(0, 1);
					strOrigin = strOrigin.substring(1, iLength);
					iLength = strOrigin.length();
				}
			}
			// DeEncrypt
			iLength = Integer.valueOf(sEncrytation[7][0]).intValue() * 10
					+ Integer.valueOf(sEncrytation[0][7]).intValue();
			for (iRow = 2; iRow <= 5; iRow++) {
				for (iCol = 0; iCol <= 7; iCol = iCol + 2) {
					sHex = sEncrytation[iRow][iCol]
							+ sEncrytation[iRow][iCol + 1];
					iAsc = new BigInteger(sHex, 16).intValue();
					result = result + String.valueOf((char) iAsc);
					iLength--;
					if (iLength <= 0) {
						iRow = 10;
						iCol = 10;
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	
	
	static String[] sSampleED = {
	      "1", "5", "6", "5", "A", "F", "D", "2", "5", "A", "8", "E", "2", "D", "6",
	      "6", "A", "2", "7", "0", "B", "8", "D", "F", "9", "C", "9", "3", "4", "8",
	      "3", "C", "F", "D", "D", "7", "6", "5", "9", "2", "F", "A", "8", "B", "E",
	      "6", "F", "5", "C", "F", "2", "0", "F", "8", "7", "8", "A", "E", "0", "1",
	      "3", "5", "F"
	      };
	
	
	/**************************************
	 * Method name		: nullToString
	 * Return type		: String
	 * Decription		:Chuyen gia tri lay tu request la null thanh ""
	 * Created date		: Jun 7, 2008
	 * Author			: Honglk
	 **************************************/
	public static String nullToString(String param){
		return (param ==null?"":param);
	}
	
	/**************************************
	 * Method name		: StringToInteger
	 * Return type		: Integer
	 * Decription		:chuyen string sang doi tuong Integer
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
	public static Integer StringToInteger(String s, int defaultValue) {
        try {
            return new Integer(s);
        } catch (Exception ex) {
            return new Integer(defaultValue);
        }
    }
	/**************************************
	 * Method name		: StringToInt
	 * Return type		: int
	 * Decription		:chuyen String sang int
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
	public static int StringToInt(String s) {
        try {
            return (new Integer(s)).intValue();
        } catch (Exception ex) {
            return -1;
        }
        
        
    }
	/**************************************
	 * Method name		: StringToInt
	 * Return type		: int
	 * Decription		:chuyen String sang int
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/

    public static int StringToInt(String s, int defaultValue) {
        try {
            return (new Integer(s)).intValue();
        } catch (Exception ex) {
            return defaultValue;
        }
    }
    /**************************************
	 * Method name		: StringToInt
	 * Return type		: int
	 * Decription		:chuyen String sang double
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
    public static double StringToDoub(String s) {
        try {
            return (Double.parseDouble(s));
        } catch (Exception ex) {
            return (Double.parseDouble("-1"));
        }

    }
    /**************************************
	 * Method name		: StringToInt
	 * Return type		: int
	 * Decription		:chuyen String sang double
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
    public static double StringToDoub(String s, double defalutValue) {
        try {
            return (Double.parseDouble(s));
        } catch (Exception ex) {
            return (defalutValue);
        }

    }
	

}


	
